using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Web.Core.Objects;
using Web.Core;

public partial class AdminCP_AdminControls_AddEditProduct : System.Web.UI.UserControl
{
    public int proID = 0;
    public string header = "";    
    string pDo = "";

    protected void Page_Load(object sender, EventArgs e)
    {        
        pDo = Request.Params["do"];
        if (pDo != null)        
            InitControl();        
    }

    void InitControl()
    {
        this.drpVisible.Items.Clear();
        this.drpSpecial.Items.Clear();
        this.drpMan.Items.Clear();
        this.drpCat.Items.Clear();

        this.drpVisible.Items.Add(Resources.Lang.Yes);
        this.drpVisible.Items.Add(Resources.Lang.No);
        this.drpSpecial.Items.Add(Resources.Lang.Yes);
        this.drpSpecial.Items.Add(Resources.Lang.No);
        this.drpPro.Items.Add(Resources.Lang.Yes);
        this.drpPro.Items.Add(Resources.Lang.No);

        foreach (Category cat in Categories.GetCategories((int)Session["Language"]))
            this.drpCat.Items.Add("(" + cat.Id + ")  " + cat.Name);
            //this.drpCat.Items.Add("-chon-");
      /*  foreach (Manufacturer man in Manufacturers.GetAllManufacturers())
            this.drpMan.Items.Add("(" + man.MID + ")  " + man.MName);*/

        // Datetime Picker
        int minYear = DateTime.Now.Year - 10;
        int maxYear = DateTime.Now.Year + 10;
        if ((int)Session["Language"] == GlobalStorage.Language.Vietnamese)
            this.scriptEpoch.InnerHtml = "<script type=\"text/javascript\" src=\"../Js/epoch_classes_vi.js\"></script>";
        else if ((int)Session["Language"] == GlobalStorage.Language.English)
            this.scriptEpoch.InnerHtml = "<script type=\"text/javascript\" src=\"../Js/epoch_classes.js\"></script>";
        this.scriptEpoch.InnerHtml += "<script type=\"text/javascript\" language=\"javascript\">var dp_cal; window.onload = function () {dp_cal  = new Epoch('epoch_popup','popup',document.getElementById('" + this.popup_container.ClientID + "'),false," + minYear.ToString() + "," + maxYear.ToString() + ");};</script>";

        if (Request.Params["pid"] != null)
            proID = int.Parse(Request.Params["pid"]);
        switch (pDo)
        {
            case "edit":
                header = Resources.Lang.Edit;
                Product proEn = Products.GetProductByID(GlobalStorage.Language.English, proID);
                Product proVi = Products.GetProductByID(GlobalStorage.Language.Vietnamese, proID);
                this.txtNameEn.Text = proEn.Name;
                this.txaDesProEn.Value = proEn.Description;
                this.txtNameVi.Text = proVi.Name;
                this.txaDesProVi.Value = proVi.Description;
                this.txtImage.Text = proEn.Image;
                this.txtPrice.Text = proEn.Price.ToString();
                if ((int)Session["Language"] == GlobalStorage.Language.Vietnamese)
                    this.popup_container.Value = Utils.ChangeDateFormat(proEn.DateAdded);
                else
                    this.popup_container.Value = proEn.DateAdded;
               // this.drpMan.Text = "(" + proEn.ManufacturerID + ")  " + Manufacturers.GetManufacturersByID(proEn.ManufacturerID).MName;
                this.drpCat.Text = "(" + proEn.CategoryID + ")  " + Categories.GetCategoryByID(proEn.CategoryID, (int)Session["Language"]).Name;
                this.txtOrdered.Text = proEn.NumOrdered.ToString();
                if (proEn.NumInStock >= 0)
                    this.txtInStock.Text = proEn.NumInStock.ToString();
                if (proEn.Visible == true)
                    this.drpVisible.SelectedIndex = 0;
                else
                    this.drpVisible.SelectedIndex = 1;
                if (proEn.Special == true)
                    this.drpSpecial.SelectedIndex = 0;
                else
                    this.drpSpecial.SelectedIndex = 1;
                if (proEn.Promotion == true)
                    this.drpPro.SelectedIndex = 0;
                else
                    this.drpPro.SelectedIndex = 1;
                break;
            case "add":
                header = Resources.Lang.Add;
                break;
            default:
                break;
        }

        // Validator        
        this.RequiredFieldValidatorNameEn.ErrorMessage = Resources.Lang.RequireNameEn + ". ";
        this.RequiredFieldValidatorNameVi.ErrorMessage = Resources.Lang.RequiteNameVi + ".";
        this.RequiredFieldValidatortxaDesProEn.ErrorMessage = Resources.Lang.Require;
        this.RequiredFieldValidatortxaDesProVi.ErrorMessage = Resources.Lang.Require;

        this.RequiredFieldValidatorPrice.ErrorMessage = Resources.Lang.Require + ". ";
        this.RegularExpressionValidatorPrice.ErrorMessage = Resources.Lang.RequireCurrency;

        this.RequiredFieldValidatorDate.ErrorMessage = Resources.Lang.Require + ". ";
        this.RegularExpressionValidatorDate.ErrorMessage = Resources.Lang.RequireDate;
        if ((int)Session["Language"] == GlobalStorage.Language.Vietnamese)
            this.RegularExpressionValidatorDate.ValidationExpression = "\\b(0?[1-9]|[12][0-9]|3[01])[- /.](0?[1-9]|1[012])[- /.](19|20)?[0-9]{2}\\b";
        else if ((int)Session["Language"] == GlobalStorage.Language.English)
            this.RegularExpressionValidatorDate.ValidationExpression = "\\b(0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2}\\b";
    }

    protected void btnOK_Click(object sender, EventArgs e)
    {
        string date = this.popup_container.Value;

        bool visible, special, promotion;
        if (this.drpVisible.Text == Resources.Lang.Yes)
            visible = true;
        else
            visible = false;
        if (this.drpSpecial.Text == Resources.Lang.Yes)
            special = true;
        else
            special = false;
        if (this.drpPro.Text == Resources.Lang.Yes)
            promotion = true;
        else
            promotion = false;

        int cID = int.Parse(this.drpCat.Text.Substring(1, this.drpCat.Text.IndexOf(")") - 1));
        //int mID = int.Parse(this.drpMan.Text.Substring(1, this.drpMan.Text.IndexOf(")") - 1));

        if ((int)Session["Language"] == GlobalStorage.Language.Vietnamese)
            date = Utils.ChangeDateFormat(date);

        int inStock = -1; // -1 tuc la vo han
        int ordered = 0; 
        try
        {
            inStock = int.Parse(this.txtInStock.Text);
        }
        catch { }
        try
        {
            ordered = int.Parse(this.txtOrdered.Text);
        }
        catch { }

        if (pDo == "edit")
        {
            Product pro = new Product(proID, cID, ordered, inStock, this.txtImage.Text, date, double.Parse(this.txtPrice.Text), visible, special);
            pro.Promotion = promotion;
            ShopContent content = new ShopContent(this.txtNameEn.Text, this.txtNameVi.Text, Utils.AdjustTinyMCEContent(this.txaDesProEn.Value), Utils.AdjustTinyMCEContent(this.txaDesProVi.Value));
            Products.UpdateProduct(pro, content);
        }
        else if (pDo == "add")
        {
            Product pro = new Product(0, cID, ordered, inStock, this.txtImage.Text, date, double.Parse(this.txtPrice.Text), visible, special);
            pro.Promotion = promotion;
            ShopContent content = new ShopContent(this.txtNameEn.Text, this.txtNameVi.Text, Utils.AdjustTinyMCEContent(this.txaDesProEn.Value), Utils.AdjustTinyMCEContent(this.txaDesProVi.Value));
            Products.InsertProduct(pro, content);
        }        
        Response.Redirect("Products.aspx");
    }
   /* protected void drpMan_SelectedIndexChanged(object sender, EventArgs e)
    {
        foreach (Category cat in Categories.getByManu(Utils.GetID(drpMan.SelectedValue), (int)Session["Language"]))
            this.drpCat.Items.Add("(" + cat.Id + ")  " + cat.Name);
    }*/
}
